<?php
/*
 * Created on 2009-7-20
 *
 * Author: guobin@yizin.net
 */

class MVC
{
    public $actions = Array();

    public function __construct($actions)
    {
        $this->actions = $actions;
    }

    public function dispatch($uri)
    {
        if ($this->actions && is_array($this->actions))
        {
            foreach ($this->actions as $pattern=>$action)
            {
                $matches = array ();
                if (preg_match($pattern, $uri, $matches))
                {
                    $action($matches);
                    break;
                }
            }
        }
    }

    public function render($view)
    {
        if (file_exists($view))
        {
            include ($view);
        }
    }

    public function redirect($url)
    {
        header(sprintf("Location: %s", $url));
    }

    public function setMessage($key, $message)
    {
        session_start();
        $_SESSION[$key] = $message;
    }

    public function getMessage($key)
    {
        session_start();
        $message = $_SESSION[$key];
        unset ($_SESSION[$key]);
        return $message;
    }

}

?>
